//
//  UIButton+YTAdd.m
//  Demo
//
//  Created by 拓客营 on 2022/3/28.
//

#import "UIButton+YTAdd.h"

@implementation UIButton (YTAdd)

static char KBButtonTopMargin;
static char KBButtonRightMargin;
static char KBButtonBottomMargin;
static char KBButtonLeftMargin;


- (void)setExpandMargin:(CGFloat)expandMargin{
    [self setExpandEdgeInsets:UIEdgeInsetsMake(expandMargin, expandMargin, expandMargin, expandMargin)];
}

- (void)setExpandSize:(CGSize)expandSize{
    [self setExpandEdgeInsets:UIEdgeInsetsMake(expandSize.height, expandSize.width, expandSize.height, expandSize.width)];
}

- (void)setExpandEdgeInsets:(UIEdgeInsets)expandEdgeInsets{
    objc_setAssociatedObject(self, &KBButtonTopMargin,   @(expandEdgeInsets.top),   OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &KBButtonLeftMargin,  @(expandEdgeInsets.left),  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &KBButtonBottomMargin,@(expandEdgeInsets.bottom),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &KBButtonRightMargin, @(expandEdgeInsets.right), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGFloat)expandMargin{
    return [self expandEdgeInsets].top;
}

- (CGSize)expandSize{
    return CGSizeMake([self expandEdgeInsets].left, [self expandEdgeInsets].top);
}

- (UIEdgeInsets)expandEdgeInsets{
    return UIEdgeInsetsMake([objc_getAssociatedObject(self, &KBButtonTopMargin) doubleValue], [objc_getAssociatedObject(self, &KBButtonLeftMargin) doubleValue], [objc_getAssociatedObject(self, &KBButtonBottomMargin) doubleValue], [objc_getAssociatedObject(self, &KBButtonRightMargin) doubleValue]);
}

- (CGRect)enlargedRect{
    NSNumber *topEdge       = objc_getAssociatedObject(self, &KBButtonTopMargin);
    NSNumber *leftEdge      = objc_getAssociatedObject(self, &KBButtonLeftMargin);
    NSNumber *bottomEdge    = objc_getAssociatedObject(self, &KBButtonBottomMargin);
    NSNumber *rightEdge     = objc_getAssociatedObject(self, &KBButtonRightMargin);
    if(topEdge && rightEdge && bottomEdge && leftEdge){
        return CGRectMake(self.bounds.origin.x - leftEdge.doubleValue,
                          self.bounds.origin.y - topEdge.doubleValue,
                          self.bounds.size.width + leftEdge.doubleValue + rightEdge.doubleValue,
                          self.bounds.size.height + topEdge.doubleValue + bottomEdge.doubleValue);
    }else{
        return self.bounds;
    }
}

- (UIView*)hitTest:(CGPoint) point withEvent:(UIEvent*)event{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds)){
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}



- (void)setEdgeInsetsStyle:(KBEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space {
    
    CGFloat margin = space;
    KBEdgeInsetsStyle status = style;
    
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    CGFloat imgWidth = self.imageView.bounds.size.width;
    CGFloat imgHeight = self.imageView.bounds.size.height;
    CGFloat labWidth = self.titleLabel.bounds.size.width;
    CGFloat labHeight = self.titleLabel.bounds.size.height;
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    
    if (labWidth < frameSize.width) {
        labWidth = frameSize.width;
    }
    labHeight = frameSize.height;
    labWidth = frameSize.width;
    CGFloat kMargin = margin/2.0;
    
    switch (status) {
        case KBEdgeInsetsStyleImageLeft://图左字右
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, -kMargin, 0, kMargin)];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, kMargin, 0, -kMargin)];
            break;
        case KBEdgeInsetsStyleImageRight://图右字左
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, labWidth + kMargin, 0, -labWidth - kMargin)];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imgWidth - kMargin, 0, imgWidth + kMargin)];
            break;
        case KBEdgeInsetsStyleImageTop://图上字下
            [self setImageEdgeInsets:UIEdgeInsetsMake(0,0, labHeight + margin, -labWidth)];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(imgHeight + margin, -imgWidth, 0, 0)];
            break;
        case KBEdgeInsetsStyleImageBottom://图下字上
            [self setImageEdgeInsets:UIEdgeInsetsMake(labHeight + margin,0, 0, -labWidth)];
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imgWidth, imgHeight + margin, 0)];
            
            break;
        default:
            break;
    }
}


- (void)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleCocor font:(UIFont *)font image:(UIImage *)image
{
    if (title) {
        [self setTitle:title forState:UIControlStateNormal];
    }
    if (titleCocor) {
        [self setTitleColor:titleCocor forState:UIControlStateNormal];
    }
    if (font) {
        [self.titleLabel setFont:font];
    }
    if (image) {
        [self setImage:image forState:UIControlStateNormal];
    }
}

- (void)setLayerBorderWithBorderColor:(UIColor *)borderColor borderWidth:(float)borderWidth
{
    [self.layer setBorderColor:borderColor.CGColor];
    [self.layer setBorderWidth:borderWidth];
}

- (void)layerFromColor:(UIColor *)leftColor toColor:(UIColor *)rightColor cornerRadius:(float)cornerRadius size:(CGSize)size {
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, size.width, size.height);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 0);
    gl.colors = @[(__bridge id)leftColor.CGColor,(__bridge id)rightColor.CGColor];
    gl.locations = @[@(0),@(1.0f)];
    
    gl.cornerRadius = cornerRadius;
    
    [self.layer insertSublayer:gl atIndex:0];
    
}

- (void)layerFromColor:(UIColor *)leftColor toColor:(UIColor *)rightColor startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint cornerRadius:(float)cornerRadius size:(CGSize)size {
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, size.width, size.height);
    gl.startPoint = startPoint;
    gl.endPoint = endPoint;
    gl.colors = @[(__bridge id)leftColor.CGColor,(__bridge id)rightColor.CGColor];
    gl.locations = @[@(0),@(1.0f)];
    
    gl.cornerRadius = cornerRadius;
    
    [self.layer insertSublayer:gl atIndex:0];
    
}

- (void)layerDownFromColor:(UIColor *)leftColor toColor:(UIColor *)rightColor cornerRadius:(float)cornerRadius size:(CGSize)size {
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, size.width, size.height);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(0, 1);
    gl.colors = @[(__bridge id)leftColor.CGColor,(__bridge id)rightColor.CGColor];
    gl.locations = @[@(0),@(1.0f)];
    
    gl.cornerRadius = cornerRadius;
    
    [self.layer insertSublayer:gl atIndex:0];
    
}

// 自定义渐变
-(void)customGradientWithCornerRadius:(float)cornerRadius size:(CGSize)size {
    [self layerFromColor:kRGB(0x6AD9AF) toColor:kRGB(0x31ADAB) cornerRadius:cornerRadius size:size];
}

- (void)layerColor:(UIColor *)color cornerRadius:(float)cornerRadius
{
    if (color) {
        
        [self.layer setBackgroundColor:color.CGColor];
    }
    [self.layer setCornerRadius:cornerRadius];
}

- (void)backgroundColor:(UIColor *)backgroundColor cornerRadius:(float)cornerRadius
{
    [self.layer setBackgroundColor:backgroundColor.CGColor];
    [self.layer setCornerRadius:cornerRadius];
    [self.layer setMasksToBounds:YES];
}

+ (void)setPathToRoundedRect:(CGRect)rect forInset:(NSUInteger)inset inContext:(CGContextRef)context
{
    // Experimentally determined
    static const NSUInteger cornerRadius = 8;

    // Unpack size for compactness, find minimum dimension
    CGFloat w = rect.size.width;
    CGFloat h = rect.size.height;
    CGFloat m = w<h?w:h;
    
    // Special case: Degenerate rectangles abort this method
    if (m <= 0) return;
    
    // Bounds
    CGFloat b = rect.origin.y;
    CGFloat t = b + h;
    CGFloat l = rect.origin.x;
    CGFloat r = l + w;

    // Adjust radius for inset, and limit it to 1/2 of the rectangle's shortest axis
    CGFloat d = (inset<cornerRadius)?(cornerRadius-inset):0;
    d = (d>0.5*m)?(0.5*m):d;
    
    // Define a CW path in the CG co-ordinate system (origin at LL)
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, (l+r)/2, t);        // Begin at TDC
    CGContextAddArcToPoint(context, r, t, r, b, d);    // UR corner
    CGContextAddArcToPoint(context, r, b, l, b, d);    // LR corner
    CGContextAddArcToPoint(context, l, b, l, t, d);    // LL corner
    CGContextAddArcToPoint(context, l, t, r, t, d);    // UL corner
    CGContextClosePath(context);                    // End at TDC
}

+(UIButton *)buttonConfig:(NSString *)title titleColor:(UIColor *)color backgroundColor:(UIColor *)bgColor fontSize:(CGFloat)fontSize {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:fontSize]];
    [btn setBackgroundColor:bgColor];
    return btn;
}

@end
